package cn.itcast.web.controller.cargo;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    //注入附件的service
    @Reference
    private ExtCproductService extCproductService;
    //注入工厂的service因为下拉框需要工厂数据
    @Reference
    private FactoryService factoryService;


    @Reference
    private ContractDao contractDao;


    @RequestMapping("list")
    public String list(String contractId,String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
            //1.根据货物id去查询附件列表，构建条件查询
        ExtCproductExample extCproductExample=new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
            //调用分页查询
        PageInfo<ExtCproduct> pageInfo =
                extCproductService.findByPage(extCproductExample, pageNum, pageSize);

        //2.根据货物对象查询厂家id信息，在通过厂家id查询厂家信息，得到下拉框数据
        //构建查询对象
        FactoryExample factoryExample=new FactoryExample();
        //注入对象条件
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        //根据条件查询工厂对象
        List<Factory> factoryList = factoryService.findAll(factoryExample);


        //3.保存数据到域中
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }


    /**
     * 增加/修改保存方法
     * 功能入口：cargo/extc/extc-list
     * 请求路径：/cargo/extCproduct/edit.do
     * 请求参数：对象
     * 响应地址：/cargo/extCproduct/list
     */

    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct){
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        //判断，用户是操作的添加方法还是修改方法
        if (StringUtils.isEmpty(extCproduct.getId())){
            extCproductService.save(extCproduct);
        }else {
            //修改
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()
                +"&contractProductId="+extCproduct.getContractProductId();
    }


    /**
     * 功能入口：/cargo/extCproduct/list
     * 请求参数：ExtCproduct
     * 请求地址： /cargo/extCproduct/toUpdate.do
     * 响应地址：extc-update.jsp
     */

    @RequestMapping("toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        //1.根据id去查询对象，因为需要回显数据
        ExtCproduct extCproduct = extCproductService.findById(id);
        //2.查询工厂数据，注入下拉框
       FactoryExample factoryExample=new FactoryExample();
       factoryExample.createCriteria().andCtypeEqualTo("附件");
       //通过条件查询工厂数据
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        //3.保存数据
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("extCproduct",extCproduct);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);


        return "cargo/extc/extc-update";
    }


    /**
     * 删除附件
     * @param id  附件id
     * @param contractId 合同id (用于跳转到列表)
     * @param contractProductId 货物id (用于跳转到列表)
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractId="+contractId
                +"&contractProductId="+contractProductId;
    }


    /**
     * 功能入口：contract-list.jsp
     * 请求路径：/cargo/contractProduct/toImport.do
     * 请求参数：contractId
     * 返回路径：contract-view.jsp
     */

    @RequestMapping("toImport")
    public String toImport(String contractId){


       request.setAttribute("contractId",contractId);
        return "cargo/contract/contract-view";
    }


}
